<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>个人资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }
    </style>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <div class="layui-form layuimini-form">
                <div>
                    <input class="layui-upload-file" type="file" accept="" name="file">
                    <div class="layui-upload-list"
                        style="display: inline-block;vertical-align: middle;margin:15px 15px 40px 132px;width: 200px;height: 200px;border-radius: 100px;border:1px dashed #1E9FFF">
                        <img class="layui-upload-img" id="demo1"
                            style="width: 200px;height: 200px;border-radius: 100px;">
                        <p id="demoText"></p>
                    </div>
                    <button type="button" class="layui-btn layui-btn-normal" id="upload"
                        style="display: inline-block;vertical-align: middle;">
                        <i class="layui-icon">&#xe67c;</i>请上传您的头像
                    </button>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">姓名：</label>
                    <div class="layui-input-block">
                        <input type="text" name="truename" lay-verify="required" lay-reqtext="姓名不能为空" id="truename"
                            placeholder="请输入您的真实姓名" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">联系方式：</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" lay-verify="required" lay-reqtext="电话不能为空"
                            placeholder="请输入您的联系方式" value="" class="layui-input" id="phone">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn"
                            id="saveBtn">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
        layui.use(['form', 'jquery', 'miniTab', 'upload'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                miniTab = layui.miniTab,
                upload = layui.upload;
            var remember = 0; //是否记住密码
            var password = '';
            var username = '';
            var strCookie = document.cookie;
            if (strCookie != '') {
                var arrCookie = strCookie.split("; ");
                for (i = 0; i < arrCookie.length; i++) {
                    var arr = arrCookie[i].split("=");
                    if (arr[0] == 'username')
                        username = arr[1];
                    else if (arr[0] == 'remember')
                        remember = 1;
                    else if (arr[0] == 'password')
                        password = arr[1];
                }
            }
            var url = '';
            $('body').ready(function () {
                var data = {
                    username: username,
                    mode: '3'
                }
                $.post('/users', data, function (re) {
                    if (re.img != '') {
                        $('#demo1').attr('src', re.img);
                    }

                    $('#truename').val(re.truename);
                    $('#phone').val(re.phone);
                })
            })

            var uploadInst = upload.render({
                elem: '#upload',
                url: '/users', //改成您自己的上传接口
                data: {
                    'username': username,
                    'url': url,
                    'mode': '1'
                },
                size: 10,
                accept: 'images',
                acceptMime: 'image/*',
                auto: false, // 取消自动提交
                bindAction: "#upload", //确认上传按钮
                field: "img", //上传的图片part名称
                choose: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                        url = result;
                        // var a=result.substring(result.indexOf(',') + 1)
                        // url = decodeURIComponent(window.atob(a).replace(/%/g, '%25'));
                        console.log(result);
                    });
                },
                done: function (rs, index, upload) {
                    if (rs.code == 200) {
                        layer.msg("修改成功");
                    }
                },
                error: function (index, upload) {
                    layer.msg("修改失败");
                }
            });
            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var remember = 0; //是否记住密码
                var password = '';
                var username = '';
                var strCookie = document.cookie;
                if (strCookie != '') {
                    var arrCookie = strCookie.split("; ");
                    for (i = 0; i < arrCookie.length; i++) {
                        var arr = arrCookie[i].split("=");
                        if (arr[0] == 'username')
                            username = arr[1];
                        else if (arr[0] == 'remember')
                            remember = 1;
                        else if (arr[0] == 'password')
                            password = arr[1];
                    }
                }
                data1 = data.field;
                data1.username = username;
                if (url == '')
                    layer.msg('请上传您的头像！');
                else {
                    var index = layer.alert(JSON.stringify(data.field), {
                        title: '最终的提交信息'
                    }, function () {
                        data1.mode = '2';
                        data1.url = url;
                        $.post('/users', data1, function (re) {
                            if (re == '1') {
                                layer.msg('修改成功！');
                            } else {
                                layer.msg('修改失败！');
                            }
                        })

                    });
                }

                return false;

            });
        });
    </script>
</body>

</html>